package log

import (
	"context"
	logProto "dh.com/shop/srv/log/proto/log"
	"github.com/micro/go-micro"
	"github.com/micro/go-micro/client"
	"github.com/micro/go-micro/server"
	"github.com/micro/go-micro/util/log"
)

//LogWrapper
func LogWrapper(client client.Client) server.HandlerWrapper {
	return func(handlerFunc server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
			pub := micro.NewPublisher("com.dh.shop.srv.log", client)

			err := pub.Publish(ctx, &logProto.LogMessage{Msg: "[" + req.Service() + " : " + req.Endpoint() + "] START"})
			if err != nil {
				log.Error(err)
			}

			errRst := handlerFunc(ctx, req, rsp)

			err = pub.Publish(ctx, &logProto.LogMessage{Msg: "[" + req.Service() + " : " + req.Endpoint() + "] END"})
			if err != nil {
				log.Error(err)
			}

			return errRst
		}
	}
}
